package com.hotelManagerSystem.service.impl;

import com.hotelManagerSystem.model.User;
import com.hotelManagerSystem.mapper.UserMapper;
import com.hotelManagerSystem.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotelManagerSystem.util.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lqx
 * @since 2021-12-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User queryByUsername(String username) {
        System.out.println(username);
        return userMapper.queryByUsername(username);
    }

    @Override
    public Map<String, Object> queryByName(String name) {
        if(name.equals("")) {
            List<User> list = userMapper.queryAll();
            Map<String, Object> map = new HashMap<>();
            map.put("userList", list);
            return map;
        }

        name = "%" + name + "%";
        System.out.println(name);
        List<User> list = userMapper.queryByName(name);
        Map<String, Object> map = new HashMap<>();
        map.put("userList", list);
        return map;
    }

    @Override
    public Map<String, Object> delUser(Integer id) {
        int flag = userMapper.delUser(id);
        Map<String, Object> map = new HashMap<>();
        if(flag == 1) {
            map.put("delInfo", "删除成功");
            map.put("code", 200);
        } else {
            map.put("delInfo", "删除失败");
            map.put("code", 500);
        }
        return map;
    }

    //登录功能
    @Override
    public Map<String, Object> login(String username, String password) {
        Map<String,Object> map = new HashMap<>();
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //判断用户是否登录
        if (!subject.isAuthenticated()){
            //未登录状态需要做的事
            //通过当前账号和密码创建安全令牌
            AuthenticationToken token = new UsernamePasswordToken(username, password);
            //使用Subject.login()方法认证，需要抛出异常，我们可以通过捕获异常判断登录失败的原因
            try {
                subject.login(token);
            }catch (IncorrectCredentialsException incorrectCredentialsException){
                map.put("code",-2);
                map.put("loginInfo", password + "：密码错误" );
                return map;
            }catch (IllegalArgumentException illegalArgumentException){
                map.put("code",-3);
                map.put("loginInfo", password + "：参数错误" );
                return map;
            }catch (UnknownAccountException unknownAccountException){
                map.put("code",-1);
                map.put("loginInfo", password + "：账号不存在" );
                return map;
            }catch (Exception exception){
                map.put("code", 500);
                map.put("loginInfo","其他异常");
                return map;
            }
        }
        //登录通过
        Object loginUser = subject.getSession().getAttribute("loginUser");
        map.put("code", 200);
        map.put("loginInfo", loginUser);
        return map;
    }

    //用户注册
    @Override
    public Map<String, Object> register(User user) {
        //查找是否有这个用户名
        User user2 = userMapper.queryByUsername(user.getUsername());
        Map<String, Object> map = new HashMap<>();
        if(user2 != null) {
            map.put("code", -1);
            map.put("registerInfo", "用户名已被注册");
            return map;
        }

        String salt = new Date().toString();
        String password = MD5Util.md5(salt, user.getPassword());
        user.setSalt(salt);
        user.setPassword(password);
        System.out.println(user);
        int flag = userMapper.addUser(user);
        if(flag == 1) {
            map.put("code", 200);
            map.put("registerInfo", "注册成功");
        } else {
            map.put("code", 500);
            map.put("registerInfo", "注册失败");
        }

        return map;
    }

    //更新用户的信息
    @Override
    public Map<String, Object> updateUser(User user) {
        //根据盐值设定密码
        String password = MD5Util.md5(user.getSalt(), user.getPassword());
        user.setPassword(password);
        System.out.println(user);
        int flag = userMapper.updateUser(user);
        System.out.println(flag);
        Map<String, Object> map = new HashMap<>();
        if(flag == 1) {
            map.put("updateInfo", "更新成功");
            map.put("code", 200);
        } else {
            map.put("updateInfo", "更新失败");
            map.put("code", 500);
        }
        return map;
    }




    /////////////////////////////黑名单
    //一:查询所有的用户列表
    @Override
    public List<User> queryUserList() {
        return  userMapper.selectList(null);
    }
    //二:黑名单功能---根据用户的id把该用户添加进黑名单
    @Override
    public int addUserById(Integer uid) {
        return userMapper.addUserById(uid);
    }
    //三:黑名单功能:根据用户的id把该该用户从黑名单中移除(即黑名单的删除)
    @Override
    public int deleteUserById(Integer uid) {
        return userMapper.deleteUserById(uid);
    }
    //四:查询所有黑名单用户
    @Override
    public List<User> selectBlackUserList() {
        return userMapper.selectBlackUserList();
    }
    //五:查询所有白名单用户
    @Override
    public List<User> selectWhiteUserList() {
        return userMapper.selectWhiteUserList();
    }
    //六:根据用户名来查询用户
    @Override
    public List<User> selectUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }

    @Override
    public int registerUser(String username, String password) {
        String salt = MD5Util.createRandomStr1();//获取盐值
        String newPassword = MD5Util.md5(salt,password);
        return userMapper.registerUser(username,newPassword,salt);
    }
}
